<?php


namespace app\model;

use think\facade\Db;
use think\Model;
use app\common\library\Translation;

class AddressModel extends Model
{
    protected $table = "store_address";

    /**
     * @param $data
     * @return AddressModel|array|mixed|string|Model|null
     * @notes 选择地址
     * @author: dongdong
     * @date: 2025/1/2 14:45
     */
    public function add($data){
        Db::startTrans();
        try{
            $Translation = new Translation();
            $param['area_id'] = $data['area_id'];
            $param['user_id'] = $data['user_id'];
            $param['param'] = json_encode($data);
            $param['create_at'] = date('Y-m-d H:i:s');

            $content = $data['content']??'';
            if(is_numeric($data['content'])){
                $param['content'] = $content;
                $param['content_ru'] = $content;
            }else{
                $content_arr = $Translation->translationSelf($content);
                $param['content'] = $content_arr['zh'];
                $param['content_ru'] = $content_arr['ru'];
            }



            $insertGetId = self::insertGetId($param);

            $data = self::find($insertGetId);
            $data['area'] = Db::connect('fast_mysql')->table('fa_area')->where(["id"=>$data['area_id']])->find();

            Db::commit();
            return $data;
        }catch (\Exception $e){
            _d($e);
            Db::rollback();
            return '';
        }
    }

    /**
     * @param $where
     * @param $field
     * @return mixed
     * @notes 返回查询字段
     * @author: dongdong
     * @date: 2025/1/2 14:47
     */
    public function search_field_value($where,$field){
        return self::where($where)->value($field);
    }

    /**
     * @param $data
     * @return array|mixed|\think\db\BaseQuery|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 城市回显
     * @author: dongdong
     * @date: 2025/1/3 15:24
     */
    public function area_arr($data){
       return $data['area'] = Db::connect('fast_mysql')->table('fa_area')->where(["id"=>$data['area_id']])->find();
    }

    /**
     * @param $data
     * @return array|mixed|\think\db\BaseQuery|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 城市缩略回显
     * @author: dongdong
     * @date: 2025/1/8 15:30
     */
    public function area_short($data){
        return Db::connect('fast_mysql')->table('fa_area')->field("id,name,name_ru,mergename,mergename_ru,country")->where(["id"=>$data['area_id']])->find();
    }



    /**
     * @param $data
     * @return AddressModel|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 当前地址
     * @author: dongdong
     * @date: 2025/1/3 15:28
     */
    public function info($data){
        return $data['address'] = self::where(["id"=>$data['address_id']])->find();
    }


    /**
     * @param $id
     * @return AddressModel|array|mixed|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 当前地址详情
     * @author: dongdong
     * @date: 2025/1/8 14:11
     */
    public function address_content($id){
        $arr = self::where(["id"=>$id])->field("id address_id,area_id,content,content_ru")->find();
        if($arr){
            $arr['area_info'] = Db::connect("fast_mysql")->table("fa_area")->field("country,name,name_ru,mergename,mergename_ru")->where(['id'=>$arr['area_id']])->find();
            return $arr;
        }else{
            return [];
        }
    }




}